﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class indextab : System.Web.UI.UserControl
{
    private int _topicnum, _curpage = 1, _num_per_page = 10, m_pagenum;
    private string _url;

    public int num_per_page
    {
        get { return _num_per_page; }
        set { _num_per_page = value; }
    }

    public int topicnum
    {
        get { return _topicnum; }
        set { _topicnum = value; }
    }
    public int curpage
    {
        get { return _curpage; }
        set { _curpage = value; }
    }
    public string url
    {
        get { return _url; }
        set { _url = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //设置当前显示号说明
        m_pagenum = topicnum / num_per_page;
        if ((topicnum % num_per_page) != 0)
        {
            m_pagenum += 1;
        }

        if(topicnum==0)
            Literal1.Text = "当前显示 0-0 共0个";
        else if (curpage == m_pagenum)
        {
            Literal1.Text = "当前显示" + ((curpage - 1) * num_per_page + 1) + "-" + topicnum + " 共" + topicnum + "个";
        }else
            Literal1.Text = "当前显示" + ((curpage-1) * num_per_page+1) + "-" + curpage * num_per_page + " 共" + topicnum + "个";
        
        //设置前后首位
        if (curpage == 1 ||curpage==0)
        {
            page_index_first.Visible = false;
            page_index_pre.Visible = false;
        }
        else
        {
            page_index_first.NavigateUrl = url + "1";
            page_index_pre.NavigateUrl = url + (curpage - 1);
        }
        if ((curpage == m_pagenum))
        {
            page_index_last.Visible = false;
            page_index_next.Visible = false;
        }
        else
        {
            
            
            page_index_last.NavigateUrl = url + m_pagenum;
            page_index_next.NavigateUrl = url + (curpage + 1);
        }
        
        //设置 数字索引
        int begin, end,i;

        if (curpage - 2 < 1) begin = 1;
            else begin = curpage - 2;
        if (curpage + 2 > m_pagenum) end = m_pagenum;
            else end = curpage + 2;

        if(end-begin<4) //少于5页
        {
            if (begin==1)
            {
                end = 5;
                if (end>m_pagenum)
                {
                    end = m_pagenum;
                }
            }else
            {
                begin = end - 4;
                if (begin<1)
                {
                    begin = 1;
                }
            }
        }

        page_index_num.Text = "";
        if (begin!=end)
        for(i=begin;i<=end;++i)
        {
            if (i!=curpage)
            {
                page_index_num.Text += "<a href=\"" + url + i + "\">" + i + "</a>";
            }else
                page_index_num.Text += "<a class=\"index_cur_num\" href=\"" + url + i + "\">" + i + "</a>";
            
        }
        
    }
}